package com.basics.common.utils.oss;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 公共 工具类
 * Created by zxy on 18/11/15.
 */
public class ToolsUtil {

    /**
     * InputStream转换为byte[]
     *
     * @param inStream
     * @return
     * @throws IOException
     */
    public static final byte[] getByteArray(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }


    public static InputStream downloadImg(String urlImg){
        try{
            URL url = new URL(urlImg);
            //打开连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式为"GET"
            conn.setRequestMethod("GET");
            //超时响应时间为10秒
            conn.setConnectTimeout(10 * 1000);
            //通过输入流获取图片数据
            InputStream is = conn.getInputStream();
            return is;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}